package demo.boat.boatSystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import demo.boat.boatSystem.entity.DocGoods;
import demo.boat.boatSystem.entity.DocManifestes;
import demo.boat.boatSystem.mapper.DocGoodsMapper;
import demo.boat.boatSystem.mapper.DocManifestesMapper;
import demo.boat.boatSystem.service.DocGoodsService;
import demo.boat.response.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 货物表 前端控制器
 * </p>
 *
 * @author boat
 * @since 2022-04-13
 */
@RestController
@RequestMapping("/goods")
public class DocGoodsController {
    @Resource
    private DocGoodsService docGoodsService;


    @GetMapping(value = {"/findTableVal/{vvyNm}/{mnfBl}", "/findTableVal/{vvyNm}"})
    public Result goodsManagePage(@RequestParam(required = true, defaultValue = "1") Integer current,
                                  @RequestParam(required = true,defaultValue = "7") Integer size,
                                  @PathVariable(value = "vvyNm", required = true) String vvyNm, @PathVariable(value = "mnfBl", required = false) String mnfBl) {
        Page<DocGoods> page = new Page<>(current, size);
        IPage<DocGoods> docGoodsPage = docGoodsService.goodsManagePage(page, vvyNm, mnfBl);
        long total = docGoodsPage.getTotal();
        List<DocGoods> records = docGoodsPage.getRecords();
        return Result.ok().data("total", total).data("records", records);
    }

    @Resource
    private DocManifestesMapper docManifestesMapper;
    @Resource
    private DocGoodsMapper docGoodsMapper;
    @PostMapping("/addData")
    public Result addData(@RequestParam("mnfBl") String strMnfBl,
                      @RequestParam("Amount") String amount,
                      @RequestParam("Barcode") String barcode,
                      @RequestParam(value = "vin", required = false) String vin) {
        try {
            String str = "2021-01-14";

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

            Date datet = null; //初始化date

            datet = sdf.parse(str);
            DocGoods docGoods = new DocGoods();
            BigDecimal b1 = new BigDecimal("0");
            docGoods.setGodDamageFg(b1);
            docGoods.setInputer("Zhangjiale");
            docGoods.setInputTm(datet);
            docGoods.setGodVin(vin);
            BigDecimal bdAmount = new BigDecimal(amount);
            docGoods.setGodAmount(bdAmount);
            docGoods.setGodBarcode(barcode);
            QueryWrapper<DocManifestes> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("MNF_BL", strMnfBl);
            DocManifestes OneMnf = docManifestesMapper.selectOne(queryWrapper1);
            docGoods.setGodMnfId(OneMnf.getMnfId());
            docGoodsMapper.insert(docGoods);
            return Result.ok();
        }
        catch (Exception e) {
            System.out.println("error messages:" + e.getMessage());
            return Result.error();
        }
    }
    @GetMapping("/delete/{godId}")
    public Result deleteData(@PathVariable("godId") String godId) {
        try {
            QueryWrapper<DocGoods> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("GOD_Id", godId);
            docGoodsMapper.delete(queryWrapper);
            return Result.ok();
        }
        catch (Exception e) {
            System.out.println("error messages:" + e.getMessage());
            return Result.error();
        }
    }
    @PostMapping("/update")
    public Result updateData(@RequestParam("godId") String godId, @RequestParam("amount") String amount,
                             @RequestParam("barcode") String barcode,
                             @RequestParam(value = "vin", required = false) String vin) {
        try {
            QueryWrapper<DocGoods> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("GOD_ID", godId);
            DocGoods docGoods = new DocGoods();
            docGoods.setGodBarcode(barcode);
            docGoods.setGodVin(vin);
            BigDecimal bdAmount = new BigDecimal(amount);
            docGoods.setGodAmount(bdAmount);
            int result = docGoodsMapper.update(docGoods, queryWrapper);
            return Result.ok();
        }
        catch (Exception e) {
            System.out.println("error messages:" + e.getMessage());
            return Result.error();
        }

    }
}

